<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_get_option_price($options, $product_quantity='1') {
    global $e107shop_db_name;
    $sql2 = new db();
    $product_quantity = (int)$product_quantity;
    foreach (explode("|", $option_ID) as $options) {
        $options = (int)$options;
        $sql2->db_Select($e107shop_db_name['product_options'], "*", "ID='".$options."'");
        while ($row2 = $sql2->db_Fetch()) {
            if ($row2['opter'] == "-") {
                $optprice .= floatval($row2['opter'].$row2['optval']);
            } else {
                $optprice .= floatval($row2['optval']);
            }
        }
    }
    return $optprice;
}

function e107shop_get_postage_price($productID, $quantity, $region=""){
    global $e107shop_db_name, $cart_id, $shop_settings, $tp;
    $productID = (int)$productID;
    $quantity  = (int)$quantity;
    $region    = $tp->toDB($region);
    $sql=new db;
    if ($shop_settings["postage_method"] == "2") 
    {
        // If the P&H is per item
        $postage = e107shop_get_product_field($productID, "postage_price") * $quantity;
        if($shop_settings['tax_inc'] == 1)
        {
            $tax_calc = ($shop_settings['tax_rate'] + 100) / 100;
            $postage = $postage / $tax_calc;
        }
    } 
    else if ($shop_settings["postage_method"] == "3")
    {
        // If the P&H is by region only
        $sql->db_Select($e107shop_db_name['postage'], "*", "region_id='".$region."'");
        while($row = $sql -> db_Fetch()){
            $postage = $row["postage_price"];
            if($row["extra"]!="") {
                $postage += $row['extra'] * ($quantity - 1);
            }
        }
        if($shop_settings['tax_inc'] == 1){
            $tax_calc = ($shop_settings['tax_rate'] + 100) / 100;
            $postage = $postage / $tax_calc;
        }
    } else if($shop_settings["postage_method"] == "4") {
        // If the P&H is by region and item price combined
        $sql->db_Select($e107shop_db_name['postage'], "*", "region_id='".$region."'");
        while($row = $sql -> db_Fetch()){
            $postage = $row["postage_price"];
        }
        $sql=new db;
        if($sql->db_Select($e107shop_db_name['cart'], "*", "cart_ID='".$cart_id."' order by cart_time")){
            while($row = $sql -> db_Fetch()){
                $postage += e107shop_get_product_field($row["productID"], "postage_price");
            }
        }
    }
    return $postage;
}

function e107shop_calculate_tax($amount, $taxrate) {
    $price = (($amount * $taxrate) / 100);
    return $price;
}

function e107shop_calculate_excl($amount, $taxrate) {
    $price = (($amount / ((100 + $taxrate) / 100)));
    return $price;
}

## to calculate the extra cost for payment method.
function e107shop_calculate_extra_payment($paymentId, $amount, &$extraPaymentInfo) {
    global $e107shop_db_name;
    $sql = new db();
    $sql->db_Select($e107shop_db_name['gateways'], "*", "identifier='".$paymentId."'");
    $row = $sql -> db_Fetch();
    $extra = 0.0;
    $extraPaymentInfo = "";

    $costType = (int) $row['field3']; //    0 = user pays no costs, 1 = user pays all costs
    if($costType && trim($row["field4"]) != "" && $paymentId != "offline") { // no extra cost for offline payments.
        $extraPaymentInfo = $row["field4"];
        
        // splitting the amount to calculate for multiple costs..
        $extraAmounts = explode("+", $row["field4"]);
        foreach($extraAmounts as $extraAmount) {
            if(strpos($extraAmount, "%") !== false) {
                $extra += (floatval(trim(substr($extraAmount, 0, strpos($extraAmount, "%")))) * (float) $amount) / 100.0;
            } else {
                $extra += floatval(trim($extraAmount));
            }
        }
    }
    return $extra;
}

function roundfix($num, $d=0) {
    $d = pow(10, $d);
    return round(ceil($num * $d * 10) / 10) / $d;
}
